﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using WeBlog.Attributes;
using WeBlog.Interfaces;
using WeBlog.Repositories;

namespace WeBlog.Models
{    
	public class UserFormViewModelBase
	{
		[Required]
		[EmailRegex]
		[DataType(DataType.EmailAddress)]
		public string Email { get; set; }

		[Required]
		public string FirstName { get; set;  }

		[Required]
		public string LastName { get; set; }

		public string OldPassword { get; set; }
		public string NewPassword { get; set; }
		public string ConfirmNewPassword { get; set; }

		[Required]
		public int RoleFlag { get; set; }

		public string FullName { get; set; }
		public int Id { get; set; }    

		public IEnumerable<Role> AvailableRoles
		{
			get
			{
				return RoleRepository.FindRoles();
			}
		}
	}

	public class CreateUserFormViewModel : UserFormViewModelBase {
		public CreateUserFormViewModel()
		{
			RoleFlag = RoleValues.Subscriber;
		}
	}

	public class EditUserFormViewModel : UserFormViewModelBase
	{
		public EditUserFormViewModel( IUnitOfWork unitOfWork, int id ) 
		{
			var userRepository = new UserRepository(unitOfWork);
			var user = userRepository.Find(id);
			if( user == null ) throw new NotFoundException();
			Id = id;
			Email = user.Email;
			FirstName = user.FirstName;
			LastName = user.LastName;
			FullName = user.FullName;
			RoleFlag = user.RoleFlag;
		}
	}

	public class MyAccountFormViewModel : UserFormViewModelBase
	{
		public MyAccountFormViewModel(User user )
		{            
			Id = user.UserId;
			Email = user.Email;
			FirstName = user.FirstName;
			LastName = user.LastName;
			FullName = user.FullName;
			RoleFlag = user.RoleFlag;
		}

		public bool IsPasswordBeingChanged()
		{
			return (!String.IsNullOrWhiteSpace(NewPassword) &&
					!String.IsNullOrWhiteSpace(OldPassword) &&
					!String.IsNullOrWhiteSpace(ConfirmNewPassword));
		}
	}
}